package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.views.ObservableScrollView;

public class DetailActivity extends AppCompatActivity implements ObservableScrollView.OnObservableScrollViewScrollChanged {


    /**
     * http://www.jianshu.com/p/7fe153e8d237
     * 该博客是思路效果差不多  但不是我的需求，记录下以后看看
     */

    private ObservableScrollView sv_contentView;
    private LinearLayout ll_topView;
    private TextView tv_topView;
    private LinearLayout ll_fixedView;

    //用来记录内层固定布局到屏幕顶部的距离
    private int mHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        sv_contentView = (ObservableScrollView) findViewById(R.id.sv_contentView);
        ll_topView = (LinearLayout) findViewById(R.id.ll_topView);
        tv_topView = (TextView) findViewById(R.id.tv_topView);
        ll_fixedView = (LinearLayout) findViewById(R.id.ll_fixedView);
        sv_contentView.setOnObservableScrollViewScrollChanged(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            //获取HeaderView的高度，当滑动大于等于这个高度的时候，需要把topView移除当前布局，放入到外层布局
            mHeight = ll_topView.getTop();
        }
    }

    @Override
    public void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt) {
        if (t >= mHeight) {
            if (tv_topView.getParent() != ll_fixedView) {
                ll_topView.removeView(tv_topView);
                ll_fixedView.addView(tv_topView);
            }
        } else {
            if (tv_topView.getParent() != ll_topView) {
                ll_fixedView.removeView(tv_topView);
                ll_topView.addView(tv_topView);
            }
        }
    }
}
